package com.xusheng.cli.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xusheng.cli.common.annotation.OperationLogAnno;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.enums.OperationType;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.likesModule.Likes;
import com.xusheng.cli.service.LikesService;
import com.xusheng.cli.tools.CurrentPlayerUserUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 点赞(Likes)表控制层
 *
 * @author whr
 * @since 2023-09-06 13:46:23
 */
@Validated
@RestController
@RequestMapping("/user/v1/likes")
public class LikesController {
    /**
     * 服务对象
     */
    @Resource
    private LikesService likesService;

    /**
     * 查询所有数据
     *
     * @param likes 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-09-06 13:46:23
     */
    @GetMapping("queryAll")
    public Result<List<Likes>> queryAll(Likes likes) {
        long userId = CurrentPlayerUserUtil.getUserId();
        likes.setUserId(userId);
        return Result.success(likesService.list(new QueryWrapper<>(likes)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page  分页对象
     * @param likes 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-09-06 13:46:23
     */
    @GetMapping("queryByPage")
    public Result<MyPage<Likes>> queryByPage(MyPage<Likes> page, Likes likes) {
        return Result.success(likesService.page(page, new QueryWrapper<>(likes)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author whr
     * @since 2023-09-06 13:46:23
     */
    @GetMapping("queryOne/{id}")
    public Result<Likes> selectOne(@PathVariable Serializable id) {
        return Result.success(likesService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param likes 实体对象
     * @return 新增结果
     * @author whr
     * @since 2023-09-06 13:46:23
     */
    @PostMapping("insert")
    @OperationLogAnno(module = "点赞模块", type = OperationType.CREATE, desc = "添加一条点赞数据")
    public Result<Object> insert(@Validated(Save.class) @RequestBody Likes likes) {
        long userId = CurrentPlayerUserUtil.getUserId();
        likes.setUserId(userId);
        long count = likesService.count(Wrappers.<Likes>lambdaQuery()
                .eq(Likes::getVideoId, likes.getVideoId())
                .eq(Likes::getUserId, userId));
        if (count > 0) return Result.error("该视频已点赞，无法重复点赞");
        return Result.isSuccess(likesService.save(likes));
    }

    /**
     * 修改数据
     *
     * @param likes 实体对象
     * @return 修改结果
     * @author whr
     * @since 2023-09-06 13:46:23
     */
    @PutMapping("update")
    @OperationLogAnno(module = "点赞模块", type = OperationType.UPDATE, desc = "修改一条点赞数据")
    public Result<Object> update(@Validated(Update.class) @RequestBody Likes likes) {
        return Result.isSuccess(likesService.updateById(likes));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author whr
     * @since 2023-09-06 13:46:23
     */
    @DeleteMapping("delete/{id}")
    @OperationLogAnno(module = "点赞模块", type = OperationType.UPDATE, desc = "删除一条点赞数据")
    public Result<Object> delete(@PathVariable Long id) {
        long userId = CurrentPlayerUserUtil.getUserId();
        Likes likes = new Likes();
        likes.setVideoId(id);
        likes.setUserId(userId);
        return Result.isSuccess(likesService.remove(Wrappers.<Likes>lambdaQuery()
                .eq(Likes::getVideoId, id)
                .eq(Likes::getUserId, userId)));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author whr
     * @since 2023-09-06 13:46:23
     */
    @DeleteMapping("batchDelete")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(likesService.batchDelete(idList, new Likes()));
    }
}

